<script type="text/javascript">
$(function(){
	var pg = App.codex.beanMappingAdd.ui, pp = pg.params || {};
	
	pp.toObj = pp.toObj || 'this';
	pp.fromObj = pp.fromObj || 'data';

	var frm = {
		toObj: pg.seek('toObj'),
		toType: pg.seek('toType'),
		fromObj: pg.seek('fromObj'),
		fromType: pg.seek('fromType'),
		autoMap: pg.seek('autoMap')
	};

	var toObjGd = $('<div class="grid"></div>').mac('grid', {
		cols : [{
			field: 'path', width: 320, title: '路径'
		}, {
			field: 'name', width: 120, title: '属性名'
		}, {
			field: 'type', width: 320, title: '类型'
		}],
		loader: {
			url: App.ctx + '/bean/prop/list.do',
			params: {
				pageNo: 1, pageSize: 50,
				beanType: pp.toType,
				recursively: true
			},
			autoLoad: true
		},
		pagerLength: 10
	});
	frm.toObj.mac('combo', {
		keyField: 'id',
		displayField: 'path',
		multiSelect: false,
		allowInput: true,
		width: 506,
		boxLeft: 0,
		boxWidth: 500,
		boxHeight: 320,
		boxEl: toObjGd,
		onSelect: function(me, sk, sd){
			var r = (sd[0] || {});
			if (r.name) {
				pp.toObj = r.path + '.' + r.name;
				me.val(pp.toObj);
			}
		}
	}).mac('combo_grid', {});
	
	var fromTypeGd = $('<div class="grid"></div>').mac('grid', {
		cols : [{
			field: 'type', title : '类型', width: 320
		},{
			field: 'name', title : '中文名', width: 120
		}],
		loader: {
			url: App.codex.ctx + '/bean/info/list.do',
			params: { pageNo: 1, pageSize: 50 },
			autoLoad: true
		},
		pagerLength: 10
	});
	frm.fromType.mac('combo', {
		keyField: 'type',
		displayField: 'type',
		multiSelect: false,
		width: 506,
		boxLeft: 0,
		boxWidth: 500,
		boxHeight: 320,
		boxEl: fromTypeGd,
		onSelect: function(me, sk, sd){
			var r = sd[0] || {};
			if (r.id) {
				pp.fromType = r.type;
			}
		}
	});
	frm.fromType.mac('combo_grid', {});
	
	frm.toType.text(pp.toType);
	frm.toObj.val(pp.toObj);
	frm.fromType.val(pp.fromType);
	frm.fromObj.val(pp.fromObj);

	function getValues(){
		return $.extend(pp, {
			toObj: frm.toObj.val(),
			fromObj: frm.fromObj.val(),
			fromType: frm.fromType.val(),
			autoMap: frm.autoMap.is(':checked')
		});
	}

	function validate(p){
// 		var x2 = App.validate2, v = App.code.validation,
// 			m = pg.find('.msg').html('');
// 		return x2(v.svc.code, frm.code)
// 			& x2(v.svc.name, frm.name);
		return true;
	}
	pg.seek('ok').click(function(){
		var pms = getValues();
		if (!validate(pms)) {
			return;
		}
		$.post(App.codex.ctx + '/bean/mapping/add.do', pms, function(rs){
			var ro = mac.eval(rs);
			if (ro.success) {
				pg.callback(ro.data);
				pg.close();
			} else {
				pg.seek('msg').html(mac.msg(ro.data, ro.params));
			}
		});
		return false;
	});
});
</script>
<div class="form">
	<div class="item">
		<span class="label">目标类型</span>
		<div class="value" name="toType"></div>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">目标对象</span>
		<div class="combo fl" name="toObj"></div>
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">来源类型</span>
		<div class="combo fl" name="fromType"></div>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">来源对象</span>
		<input type="text" name="fromObj" />
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label"></span>
		<input type="checkbox" name="autoMap" />
		<div class="value">按属性名自动映射</div>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">&nbsp;</span>
		<div name="msg"></div>
		<div class="clear"></div>
	</div>
	<div class="bottom">
		<button name="ok" class="fr">保存</button>
	</div>
</div>